<?php 
class SysConfig extends CI_Model 
{
	const tablename = 'sysconfig';
	const fkey = 'configid';

	var $configid; 
	var $namespace; 
	var $keyconfig;
	var $valueconfig;
	


	public function __construct($mid='')
	{	
		
		if($mid!='')
			$this->LookUp($mid);
	}

	function Update()
	{		
		$this->lstupdateby=$this->loginuser['uname'];
		$this->lstupdate=date('Y-m-d H:i:s');		
		$this->db->where('uname', $this->uname);
		return $this->db->update($this->db->dbprefix($this->tablename), $this); 
	}
	
	function Create($id)
	{	
		$this->createdby=$this->loginuser['uname'];
		$this->created=date('Y-m-d H:i:s');
		return $this->db->insert($this->db->dbprefix($this->tablename), $this); 
	}

	function LookUp($id)
	{
		$rw= $this->Inquiry($id);	
		if(count($rw)>0){				
			$this->configid=$rw['configid'];
			$this->namespace=$rw['namespace'];
			$this->keyconfig=$rw['keyconfig'];
			$this->valueconfig=$rw['valueconfig'];			
		}
		else{
			return false;
		}
	}

	function mapper(&$obj,$rw)
	{
			$obj->configid=$rw['configid'];
			$obj->namespace=$rw['namespace'];
			$obj->keyconfig=$rw['keyconfig'];
			$obj->valueconfig=$rw['valueconfig'];	
	}

	function GetConfigByKey($k)
	{
		$rslt=self::Inquiry(false,false,$k);
		if(count($rslt)>0){						
			return new SysConfig($rslt['configid']);
		}
		else{
			return false;
		}
	}

	function GetListItem($nmspace)
	{
		$rslt=$this->Inquiry(false,$nmspace);
		if($rslt)
		{			
			 $lst=array();
			foreach($rslt as $k=>$v){				
				$lst[$v['keyconfig']]=$v['valueconfig'];
			}			
			
			return $lst;
		}
		else{		
			return false;
		}
	}

	function Delete($id)
	{
		$this->db->where_in('uname', $id);
		return $this->db->delete($this->db->dbprefix($this->tablename)); 		
	}


	
	public function GetTreeMenu($var)
	{
		$menu=array();		
		foreach($var as $row)
		{			
			$menu =array_merge($menu,$this->get_menu($row['moduleid']));
		}				
		return $menu;
	}	
	
	private function get_menu($var)
	{	
		$where=array('menuheader'=>$var);		
		$this->db->order_by('menuindex');

		$query = $this->db->get_where($this->db->dbprefix('sysmenu'), $where, 25, 0);
		$res= $query->result_array();		
		if($res)
		foreach ($res as &$value) {
			$value['childs']=array();			
			$where=array('menuheader'=>$value['menuid']);			
			$query2 = $this->db->get_where($this->db->dbprefix('sysmenu'), $where, 25, 0);								
			if($query2) 
			{	
				$res2=$this->get_menu($value['menuid']);
				$value['childs']=$res2;				
			}						
		}
		return $res;
	}

	public function Inquiry($mid = false,$nmspc=false,$key=false,$order=false)
	{	
		
		if($mid)
		 $this->db->where($this::fkey,$mid);	
		
		if($nmspc)
		 $this->db->where('namespace',$nmspc);

		if($key)
		 $this->db->where('keyconfig',$key);

		if($order)
		$this->db->order_by($order);

		$query = $this->db->get($this->db->dbprefix(self::tablename));		
				
		if ($mid === false && $key === false)
		{			
		//	$query = $this->db->get($this->db->dbprefix($this->tablename));				
			return $query->result_array();
		}
		else
		{
			//$query = $this->db->get_where($this->db->dbprefix($this::tablename), array('menuid' => $mid));			
			return $query->row_array();
		}
	} 

}



?>



